package com.lekang.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.system.domain.LeaveRequest;
import com.lekang.system.service.ILeaveRequestService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 请假Controller
 * 
 * @author zhihao
 * @date 2025-10-14
 */
@RestController
@RequestMapping("/system/request")
public class LeaveRequestController extends BaseController
{
    @Autowired
    private ILeaveRequestService leaveRequestService;

    /**
     * 查询请假列表
     */
    @PreAuthorize("@ss.hasPermi('system:request:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveRequest leaveRequest)
    {
        startPage();
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList(leaveRequest);
        
        // 过滤掉被拒绝的请假申请（leaveStatus = '拒绝'）
        // 被拒绝的申请只在审批记录中显示，不在请假管理中显示
        list = list.stream()
            .filter(request -> {
                String status = request.getLeaveStatus();
                boolean isNotRejected = !"拒绝".equals(status);
                if (!isNotRejected) {
                    System.out.println("过滤掉被拒绝的请假申请: ID=" + request.getId() + ", 状态=" + status);
                }
                return isNotRejected;
            })
            .collect(java.util.stream.Collectors.toList());
        
        System.out.println("请假管理列表 - 原始数量: " + leaveRequestService.selectLeaveRequestList(leaveRequest).size() 
            + ", 过滤后数量: " + list.size());
        
        return getDataTable(list);
    }

    /**
     * 导出请假列表
     */
    @PreAuthorize("@ss.hasPermi('system:request:export')")
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveRequest leaveRequest)
    {
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList(leaveRequest);
        ExcelUtil<LeaveRequest> util = new ExcelUtil<LeaveRequest>(LeaveRequest.class);
        util.exportExcel(response, list, "请假数据");
    }

    /**
     * 获取请假详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:request:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveRequestService.selectLeaveRequestById(id));
    }

    /**
     * 新增请假
     */
    @PreAuthorize("@ss.hasPermi('system:request:add')")
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveRequest leaveRequest)
    {
        return toAjax(leaveRequestService.insertLeaveRequest(leaveRequest));
    }

    /**
     * 修改请假
     */
    @PreAuthorize("@ss.hasPermi('system:request:edit')")
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveRequest leaveRequest)
    {
        return toAjax(leaveRequestService.updateLeaveRequest(leaveRequest));
    }

    /**
     * 删除请假
     */
    @PreAuthorize("@ss.hasPermi('system:request:remove')")
    @Log(title = "请假", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveRequestService.deleteLeaveRequestByIds(ids));
    }


}
